package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.UserDao;
import cn.ym.mall.entity.User;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.management.Query;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public int addUser(User user) {
        int num = 0;
        try{
            String sql = "insert into ym_user( grid, mbscore, uname, upwd, usex, uphone, uemail,\n" +
                            "uaddress, uavatars, uidcard, ustate) value (?,?,?,?,?,?,?,?,?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,user.getGrid(),user.getMbscore(),user.getUname(),
                                    user.getUpwd(),user.getUsex(),user.getUphone(),user.getUemail(),user.getUaddress(),
                                    user.getUavatars(),user.getUidCard(),user.getUstate());
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查看个人信息
     *
     * @param uid
     * @return
     */
    @Override
    public User getUserByUid(Integer uid) {
        User user = new User();
        try {
            String sql = "select * from ym_user where uid=?;";
            user = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<User>(User.class),uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 修改个人信息
     *
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        int num = 0;
        try {
            String sql = "update ym_user set grid=?,upwd = ?, uphone = ?, uemail=?, uaddress=?, uavatars=?, unickname=? where uid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,
                    user.getGrid(),user.getUpwd(),user.getUphone(),user.getUemail(),user.getUaddress(),user.getUavatars(),user.getUid());
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @Override
    public List<User> getAllUser() {
        List<User> userList = new ArrayList<>();
        try {
            String sql = "select * from ym_user;";
            userList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<User>(User.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return userList;
    }

    /**
     * 查询用户状态
     *
     * @param ustate
     * @return
     */
    @Override
    public User getUserByustate(Integer ustate) {
        User user = new User();
        try {
            String sql = "select ustate from ym_user where uid=?;";
            user = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<User>(User.class),ustate);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 分页查询用户
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<User> getUserPage(Integer pageNo, Integer pageSize) {
        List<User> userList = new ArrayList<>();
        try {
            String sql = "select uid,grid,mbscore,uname from ym_user order by uid desc limit ?,?;";
            userList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<User>(User.class),(pageNo-1)*pageSize,pageSize);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userList;
    }

    /**
     * 查询用户总数
     *
     * @return
     */
    @Override
    public int getUserCount() {
        int count = 0;
        try {
            String sql = "select count(0) as count from ym_user;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>())).intValue();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过uid删除用户
     *
     * @param uid
     * @return
     */
    @Override
    public int deleteUser(Integer uid) {
        int num = 0;
        try {
            String sql = "delete from ym_user where uid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过用户名查找
     *
     * @param uname
     * @return
     */
    @Override
    public User getUserByName(String uname) {
        User user = null;
        try {
            String sql = "select * from ym_user where uname=?;";
            user = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<User>(User.class),uname);
        }catch (Exception e){
            e.printStackTrace();
        }
        return user;
    }

}
